//------------------------------------------------------------------------------
// <copyright company="Telligent Systems">
//     Copyright (c) Telligent Systems Corporation.  All rights reserved.
// </copyright> 
//------------------------------------------------------------------------------

using System;
using System.Collections;
using System.Collections.Specialized;
using System.Text;
using System.Text.RegularExpressions;

namespace CommunityServer.Components
{

	public class ApplicationKeyMapping
	{

		public ApplicationKeyMapping(string locationName, string pattern)
		{
			_locationName = locationName;
			regex = new Regex(pattern, RegexOptions.IgnoreCase|RegexOptions.Compiled);
		}

		private string _locationName = null;
		private Regex regex = null;

		public string LocationName
		{
			get {return _locationName;}
		}

		public bool IsMatch(string url)
		{
			return regex.IsMatch(url);
		}

	}

    public class LocationSet : IEnumerable
    {
        ListDictionary locations = new ListDictionary();

        public void Add(string name, Location location)
        {
            
            
            locations[name.ToLower()] = location;
        }

        public string this[string name]
        {
            get
            {
                return ((Location)locations[name.ToLower()]).Path;
            }
        }

        public Location FindLocationByPath(string path)
        {
			ArrayList mappings = Globals.GetSiteUrls().Mappings;
			foreach(ApplicationKeyMapping map in mappings)
			{
				if(map.IsMatch(path))
				{
					return FindLocationByName(map.LocationName);
				}
			}

            foreach(Location loc in locations.Values)
            {
                if(loc.IsMatch(path))
                {
                    return loc;
                }
            }

            return null;
        }

        public Location FindLocationByName(string name)
        {
            name = name.ToLower();
            foreach(string key in locations.Keys)
            {
                if(key == name)
                    return this.locations[name] as Location;
            }

            return null;
        }
        #region IEnumerable Members

        public IEnumerator GetEnumerator()
        {
            return locations.GetEnumerator();
        }

        #endregion

        public string Filter
        {
            get
            {
                StringBuilder sb = new StringBuilder();
                foreach(Location loc in locations.Values)
                {
                    if(loc.Exclude &&  loc.Path != null && loc.Path.Length > 1)
                    {
                        sb.Append("|" + loc.Path);
                    }
                }

                //return sb.ToString();

                string text = sb.ToString();
                if(text != null && text.Length > 0)
                    text = text.Substring(1);

                return string.Format("^({0})", text);
            }
        }
    }



    public class Location : IEnumerable
    {
        private ArrayList _urls = new ArrayList();

        public void Add(ReWrittenUrl url)
        {
            _urls.Add(url);
        }

        public int Count
        {
            get{ return _urls.Count;}
        }

        public IEnumerator GetEnumerator()
        {
            return _urls.GetEnumerator();
        }

        public Location(string path, string physicalPath, bool exclude)
        {
            _path = path;
            if(physicalPath == null)
                _physicalPath = path;
            else
                _physicalPath = physicalPath;

            _exclude = exclude;

            if(!Globals.IsNullorEmpty(path) )
            {
                _regex = new Regex("^" + path,RegexOptions.IgnoreCase | RegexOptions.Compiled);
            }
        }


        private Regex _regex=null;
        private string _path;
        private string _physicalPath;
        private bool _exclude = false;

        public bool Exclude
        {
            get { return _exclude;}
        }

        public bool IsMatch(string url)
        {
            if(_regex == null)
                return false;

            return _regex.IsMatch(url);
        }

        public string PhysicalPath
        {
            get
            {
                return _physicalPath;
            }
        }

        public string Path
        {
            get { return _path;}
        }

        public virtual string ReWriteUrl(string path, string queryString)
        {
            string newPath = null;
            if(this.Count > 0)
            {
                foreach(ReWrittenUrl url in this)
                {
                    if(url.IsMatch(path))
                    {
                        newPath = url.Convert(path,queryString);
                        CSContext.Current.RewrittenUrlName = url.Name;
                        break;
                    }
                    
                }
            }

            return newPath;
        }
    }


    public class ReWrittenUrl
    {
        private string _name; 
        private string _path;
        private string _pattern;
        private Regex _regex=null;
        
        public ReWrittenUrl(string name, string pattern, string path)
        {
            _name = name;
            _path = path;
            _pattern = "^" + pattern;
            _regex = new Regex(_pattern,RegexOptions.IgnoreCase | RegexOptions.Compiled);
        }

        public bool IsMatch(string url)
        {
            return _regex.IsMatch(url);
        }

        public string Pattern
        {
            get { return _pattern;}
        }

        public string Name
        {
            get{return _name;}
        }

        public string Path
        {
            get{return _path;}
        }

        public virtual string Convert(string url, string qs)
        {
            if(qs != null && qs.StartsWith("?"))
            {
                qs = qs.Replace("?","&");
            }
            return string.Format("{0}{1}", _regex.Replace(url, _path),qs);
        }
    }
}